package com.ruoyi.nonghu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;

import com.ruoyi.nonghu.domain.Crop;
import com.ruoyi.nonghu.domain.Logs;
import com.ruoyi.nonghu.domain.TraceTransport;
import com.ruoyi.nonghu.domain.TraceUserCrops;
import com.ruoyi.nonghu.service.CropService;
import com.ruoyi.nonghu.service.LogsService;
import com.ruoyi.nonghu.service.TraceTransportService;
import com.ruoyi.nonghu.service.TraceUserCropsService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("honghu")
@CrossOrigin
public class NonghuController {

    @Autowired
    CropService cropService;


    @Autowired
    TraceUserCropsService traceUserCropsService;


    @Autowired
    LogsService logsService;


    @RequestMapping("add")
    public Map<String,Object> add(@RequestBody Crop crop){
        HashMap<String, Object> map = new HashMap<>();
        //添加农作物表
        //int i =  cropService.savaCrop(crop);
        boolean save = cropService.save(crop);

        //给农作物基础表
        TraceUserCrops traceUserCrops2 = new TraceUserCrops();

        if (save){
            traceUserCrops2.setCropsId(crop.getId()+"");
            traceUserCrops2.setCropsName(crop.getName());
            String token = crop.getToken();
            Claims claims = JwtUtils.parseToken(token);
            String username = (String) claims.get("username");
            traceUserCrops2.setUsername(username);
            traceUserCrops2.setStatus(0);
            traceUserCropsService.save(traceUserCrops2);
            map.put("smg","添加成功");
            return map;
       }
        map.put("smg","失败");
       return map;
    }


    @RequestMapping("list")
    public Page<TraceUserCrops> list(Page<TraceUserCrops> page){
        return traceUserCropsService.page(page);
    }

  /*  @RequestMapping("test")
    public void test(String token){
        System.out.println(token);
        Claims claims = JwtUtils.parseToken(token);
        System.out.println(claims);
    }*/




    @RequestMapping("sava")
    public Map<String,Object> sava(@RequestBody Logs logs){
        HashMap<String, Object> map = new HashMap<>();
        logs.setCropId(logs.getCropId());
        boolean flag = logsService.save(logs);
        map.put("flag",flag);
        return map;
    }



    @RequestMapping("upPic")
    public String upPic(MultipartFile file) throws IOException {

        if (null!=file && file.getSize()>0){
            String filename = file.getOriginalFilename();
            String s = UUID.randomUUID() + filename.substring(filename.lastIndexOf("."));
            File file1 = new File("D:\\pic", s);
            file.transferTo(file1);
            return "/pic/"+s;
        }
        return null;
    }


    @RequestMapping("logsList")
    public List<Logs> logsList(Integer cropId){
        return logsService.logsList(cropId);
    }



//    @RequestMapping("tracList")
//    public List<TraceUserCrops> tracList(Integer cropsId){
//        System.err.println(cropsId);
//         return traceUserCropsService.tracList(cropsId);
//    }

    @RequestMapping("tracList")
    public R tracList(Integer cropsId){
        System.err.println(cropsId);
        return R.ok(traceUserCropsService.tracList(cropsId));
    }


    @Autowired
    TraceTransportService traceTransportService;

    /**
     * 添加运输表+更改作物状态
     * @return
     */
    @RequestMapping("addtransport")
    public R addtransport(@RequestBody TraceTransport traceTransport){
        TraceTransport tracetransport= new TraceTransport();
        tracetransport.setCropsId(traceTransport.getCropsId());
        tracetransport.setDriverId(traceTransport.getDriverId());
        tracetransport.setTime(new Date());
        tracetransport.setFarmerNickName("李杰");
        tracetransport.setFarmerUserName("lijie");
        tracetransport.setFarmerTel("15049819045");
        tracetransport.setFarmerDept(traceTransport.getFarmerDept());
        tracetransport.setFactoryId(129);
        tracetransport.setStatus(0);//0 未运输 1 正在运输  2运输完成
        tracetransport.setRemarks(traceTransport.getRemarks());

        boolean save = traceTransportService.save(tracetransport);
        System.out.println(save?"运输记录成功添加":"运输记录添加失败");

        //更改作物状态
        LambdaQueryWrapper<TraceUserCrops> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TraceUserCrops::getCropsId,traceTransport.getCropsId());

        TraceUserCrops one = traceUserCropsService.getOne(wrapper);
        one.setStatus(1);
        boolean b = traceUserCropsService.updateById(one);
        System.out.println(b?"作物状态已改":"作物状态更改失败");

        return save?R.ok():R.fail();

    }

}
